using System.Collections.Generic;
using System.Linq;
using Android.Content;
using MuseumsBC.Models;
using SQLite;

namespace MuseumsBC.DataAccess
{
    public class OrmMuseumRepository : IMuseumRepository
    {
        private MuseumDatabaseHelper _helper;

        public OrmMuseumRepository(Context context)
        {
            _helper = new MuseumDatabaseHelper(context);
        }

        public IList<Museum> GetAllMuseums()
        {
            using (var database = new SQLiteConnection(_helper.WritableDatabase.Path))
            {
                return database
                        .Table<Museum>()
                        .ToList();
            }
        }

        public Museum GetMuseumById(int museumId)
        {
            using (var database = new SQLiteConnection(_helper.WritableDatabase.Path))
            {
                return database.Table<Museum>().Where(m => m.Id == museumId).FirstOrDefault();
            }
        }

        public long AddMuseum(string title, string contents)
        {
            using (var database = new SQLiteConnection(_helper.WritableDatabase.Path))
            {
                return database.Insert(new Museum
                {
                    Title = title,
                    Contents = contents
                });
            }
        }
        public long AddMuseum(Museum museum)
        {
            using (var database = new SQLiteConnection(_helper.WritableDatabase.Path))
            {
                return database.Insert(new Museum
                {
                    Address = museum.Address,
                    ChinCode = museum.ChinCode,
                    ChinUrl = museum.ChinUrl,
                    Contents = museum.Contents,
                    Description = museum.Description,
                    HoursOfOperation = museum.HoursOfOperation,
                    Id = museum.Id,
                    Image = museum.Image,
                    Latitude = museum.Latitude,
                    Longitude = museum.Longitude,
                    MuseumName = museum.MuseumName,
                    Telephone = museum.Telephone,
                    Title = museum.Title,
                    Web = museum.Web
                });
            }
        }
    }
}